<template> 
  <div class="app-container">
    <smart-filter :list="filterList" :cutEmpty="false" @setFilter="getFilter"></smart-filter>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <el-button size="mini" class="btn-add" @click="handleAdd()" style="margin-left: 20px">添加品牌</el-button>
    </el-card>
    <div class="table-container">
      <el-table ref="flashTable"
        :data="list"
        style="width: 100%;"
        v-loading="listLoading" border>
        <el-table-column type="selection" width="60" align="center"></el-table-column>
        <el-table-column label="编号" width="200" align="center">
          <template slot-scope="scope">{{scope.row.brandCode}}</template>
        </el-table-column>
        <el-table-column label="品牌名称" align="center" width="200">
          <template slot-scope="scope">{{scope.row.brandName}}</template>
        </el-table-column>
        <el-table-column label="入驻" width="200" align="center">
          <template slot-scope="scope">{{scope.row.status}}</template>
        </el-table-column>
        <el-table-column label="创建人" width="200" align="center">
          <template slot-scope="scope">{{scope.row.creator}}</template>
        </el-table-column>
        <el-table-column label="创建时间" width="200" align="center">
          <template slot-scope="scope">{{scope.row.createDate}}</template>
        </el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button size="mini"
              type="text"
              @click="handleSelectSession(scope.row)">产品维护
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="pageNum"
        :page-size="pageSize"
        :page-sizes="[5,10,15]"
        :total="total">
      </el-pagination>
    </div>
    <el-dialog
      title="添加品牌"
      :visible.sync="dialogVisible"
      width="40%">
      <el-form :model="brand"
        ref="flashPromotionForm"
        label-width="150px" size="small">
        <el-form-item label="品牌名称：">
          <el-input v-model="brand.brandName" style="width: 250px"></el-input>           
        </el-form-item> 
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="handleDialogConfirm()" size="small">保存</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="添加产品"
      :visible.sync="goodsdialogVisible"
      width="60%">
      <el-form>
        <el-form-item label="产品：">
          <el-select
            v-model="product.productCode"
            placeholder="请选择产品"
            clearable
            @visible-change="choseProduct(product.productCode)"
          >
            <el-option
              v-for="item in productOptions"
              :key="item.productCode"
              :label="item.productName"
              :value="item.productCode"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div>
        <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">全选</el-checkbox>
        <div style="margin: 15px 0;"></div>
        <el-checkbox-group v-model="checkedGoods" @change="handlecheckedGoodsChange">
          <el-checkbox v-for="goodsvo in goodsvoList" :label="goodsvo.goodsCode" :key="goodsvo.goodsCode">{{goodsvo.specDescription}}</el-checkbox>
        </el-checkbox-group>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="goodsdialogVisible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="handleAddBrandGoodsDialogConfirm()" size="small">保存</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import SmartFilter from '@/components/common/filter/index.vue'
import SmartTable from '@/components/common/table/index.vue'
  import {fetchList,getBrandGoodsByProduct, updateStatus,createbrand,getBrandList, deleteFlash, updateFlash,brandAddProductCheckbox,updateBrandGoods} from '@/api/brand';
  import {formatDate} from '@/utils/date';
  import{getAllProducts,getGoodsByProduct} from '@/api/goods';
  import {
  getProductListByUserId
} from "@/api/product";
  const cityOptions = [];
  const defaultFlashPromotion = {
    id: null,
    title: null,
    startDate: null,
    endDate: null,
    status: 0
  };
  export default {
    name: 'flashPromotionList',
    components: {SmartFilter, SmartTable},
    data() {
      return {
        filterList: [
          {
            label: '品牌名称',
            key: 'brandName',
            type: 'text',
            defaultValue: '',
            action: ''
          }
        ],
        pageNum: 1,
        pageSize: 5,
        listQuery: {},
        list: null,
        total: null,
        listLoading: false,
        dialogVisible: false,
        goodsdialogVisible:false,
        brand: Object.assign({}, defaultFlashPromotion),
        isEdit: false, 
        product:{},
        productOptions:[] ,
        selectBrand:{},

         checkAll: false,
        checkedGoods: [],
        goodsvoList: cityOptions,
        isIndeterminate: true

      }
   
    },
    created() {
      this.getList();
    },
    filters: {
      formatActiveStatus(row) {
        // let nowTime = new Date().getTime();
        // if (nowTime >= row.startDate && nowTime <= row.endDate) {
        //   return '活动进行中';
        // } else if (nowTime > row.endDate) {
        //   return '活动已结束';
        // } else {
        //   return '活动未开始';
        // }
      },
      formatDate(time) {
        if (time == null || time === '') {
          return 'N/A';
        }
        let date = new Date(time);
        return formatDate(date, 'yyyy-MM-dd')
      }
    },
    methods: {
      getFilter (params) {
        this.listQuery.queryObject = params
        this.getList()
      },
       handleCheckAllChange(val) {
        if(val==true){
          this.goodsvoList.forEach((item) => {
            this.checkedGoods.push(item.goodsCode);
          })
        }else{
          this.checkedGoods=[]
        }
        this.isIndeterminate = false;
      },
      handlecheckedGoodsChange(value) {
        let checkedCount = value.length;
        this.checkAll = checkedCount === this.goodsvoList.length;
        this.isIndeterminate = checkedCount > 0 && checkedCount < this.goodsvoList.length;
      },
      handleSizeChange(val) {
        this.pageNum = 1;
        this.pageSize = val;
        this.getList();
      },
      handleCurrentChange(val) {
        this.pageNum = val;
        this.getList();
      },
      handleAdd() {
        this.dialogVisible = true;
        getProductListByUserId({pageNum:1,pageSize:100}).then(response=>{
          if(response.code=='200'){  this.productOptions=response.data.list}       
        })
      },
      handleShowSessionList() {
        this.$router.push({path: '/sms/flashSession'})
      },
      handleStatusChange(index, row) {
        this.$confirm('是否要修改该状态?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          updateStatus(row.id, {status: row.status}).then(response => {
            this.$message({
              type: 'success',
              message: '修改成功!'
            });
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消修改'
          });
          this.getList();
        });
      },
      handleDelete(index, row) {
        this.$confirm('是否要删除该活动?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteFlash(row.id).then(response => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.getList();
          });
        });
      },
      handleUpdate(index, row) {
        this.dialogVisible = true;
        this.isEdit = true;
        this.flashPromotion = Object.assign({},row);
      },
      handleDialogConfirm() {
        this.$confirm('是否要确认?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          createbrand(this.brand).then(response => {
              this.$message({
                message: '添加成功！',
                type: 'success'
              });
              this.dialogVisible =false;
              this.getList();
            })
          
        }
                 
        )},

           handleAddBrandGoodsDialogConfirm() {
        this.$confirm('是否要确认?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
      var tem={}
          tem.productCode = this.product.productCode
          tem.brandCode = this.selectBrand.brandCode
          tem.goodsCodeList = this.checkedGoods

          updateBrandGoods(tem).then(response => {
            this.goodsdialogVisible=false
                   this.productOptions=[]
        this.product={}
        this.goodsvoList=[];
              this.$message({
                message: '添加成功！',
                type: 'success'
              });
              this.dialogVisible =false;
              this.getList();
            })
          
        }
                 
        )},

        
      // handleSelectSession(index,row){
      //   this.productOptions=[]
      //   this.product={}
      //   this.goodsvoList=[];
      //   this.selectBrand=row;
      //   this.goodsdialogVisible = true
      //  getProductListByUserId({pageNum:1,pageSize:100}).then(response=>{
      //     if(response.code=='200'){  this.productOptions=response.data.list}       
      //   })
      // },
      handleSelectSession (row) {
        this.$router.push({path: '/cbkhc/productSet', query: {code: row.brandCode, name: row.brandName}})
      },
      choseProduct(val){
        if(val==undefined){
          return
        }
       var tem={}
       tem.productCode=val
       tem.brandCode=this.selectBrand.brandCode;
      brandAddProductCheckbox(tem).then(response=>{ 
        this.checkedGoods=[];
        if(response.code=='200') {
        this.goodsvoList=response.data  
          this.goodsvoList.forEach((item) => {
            if(item.check=='1'){
        this.checkedGoods.push(item.goodsCode);
        }
})

          
        }
      })
      },

      getList() {
        this.listLoading = true;
        this.listQuery.pageNum = this.pageNum
        this.listQuery.pageSize = this.pageSize
        fetchList(this.listQuery).then(response => {
          this.listLoading = false;
          this.list = response.data.list;
          this.total = response.data.total;
        });
      }
    }
  }
</script>
<style></style>


